<?php

namespace frontend\modules\goods\controllers;

use api\modules\v1\models\Wxuser;
use backend\modules\goods\models\AttrvalueGoods;
use frontend\modules\base\controllers\BaseController;
use frontend\modules\goods\models\Collect;
use frontend\modules\marketing\models\ShareReb;
use frontend\modules\orders\models\OrderGoods;
use Yii;
use frontend\modules\goods\models\Goods;
use frontend\modules\goods\models\search\GoodsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\token\tokenCheck;

/**
 * 获取商品信息，商品列表，商品属性，商品热销等
 *
 * Class GoodsController
 * @package frontend\modules\goods\controllers
 */
class GoodsController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public $enableCsrfValidation = false;

    /**
     * 获取商品列表
     * @desc 获取商品列表，此接口无需传递任何验证信息
     *
     * @return string message 提示信息
     * @return string data 列表数据
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 202 数据为空
     */
//    public function actionGoodslist()
//    {
//        $goodsList = Goods::find()->where(['is_delete'=>0])->asArray()->all();
//        if($goodsList){
//            $message = ['code'=>200,'message'=>'成功获取','data'=>$goodsList];
//            return json_encode($message);
//        }else{
//            $message = ['code'=>202,'message'=>'数据为空','data'=>$goodsList];
//            return json_encode($message);
//        }
//    }

    /**
     * 这是一个测试的Api
     * @desc 列举所有的注释格式
     * @param string $user_type |用户类型|yes|其他说明|
     * @param int $sex |性别|no|0:不限 1:男 2:女|
     * @return int status 操作码，0表示成功
     * @return array list 用户列表
     * @return int list[].id 用户ID
     * @return string list[].name 用户名字
     * @return string msg 提示信息
     * @exception 400 参数传递错误
     * @exception 500 服务器内部错误
     *
     */
    public function actionDemoapi()
    {
        Yii::$app->request->post();
        $result = [
            'status' => 0,
            'list' => [
                'id' => 1,
                'name' => 'kaopur'
            ],
            'msg' => 'OK'
        ];
        return \yii\helpers\Json::encode($result);
    }

    /**
     * 获取商品详情页信息
     *
     * @desc 一个商品多个属性，商品先分类，每一分类多个属性键，每个键多个属性值，一个商品一个属性值，但拥有多个属性，数据包括商品主图片，长图等
     * @param int $goods_id |商品ID|yes|
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionGoodinfo()
    {
        header("Access-Control-Allow-Origin:*");
        $post = Yii::$app->request->post();
        $goods_id = isset($post['goods_id']) ? $post['goods_id'] : '';
        if ($goods_id) {
            $goods = Goods::find()->where(['goods_id' => $goods_id])->asArray()->one();
            $goodsInfo = Goods::getGoodsInfo($goods);

            if(strpos($goodsInfo['goods_name'],'P8') !== false||strpos($goodsInfo['goods_name'],'P8') ==0){
                $goodsInfo['Typename'] = 'P8';
            } else if(strpos($goodsInfo['goods_name'],'P7') !== false ||strpos($goodsInfo['goods_name'],'P7') ==0){
                $goodsInfo['Typename'] = 'P7';
            }

            $desc_text = isset($goodsInfo['desc_text'])?$goodsInfo['desc_text']:null;
            if($desc_text) {
                $goodsImages = explode('--',$goodsInfo['desc_text']);
                $goodsInfo['desc_text'] = $goodsImages;
            }

            $desc_slt = isset($goodsInfo['desc_slt'])?$goodsInfo['desc_slt']:null;
            if($desc_slt) {
                $goodsImages = explode('--',$goodsInfo['desc_slt']);
                $goodsInfo['desc_slt'] = $goodsImages;
            }


            $message = ['code' => 200, 'message' => '获取成功', 'data' => $goodsInfo];
            return json_encode($message);
        } else {
            $message = ['code' => 500, 'message' => '缺少参数'];
            return json_encode($message);
        }
    }

    /**
     * 获取商品属性信息
     *
     * @desc 一个商品多个属性，商品先分类，每一分类多个属性键，每个键多个属性值，一个商品一个属性值，但拥有多个属性
     * @param int $goods_id |商品ID|yes|
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionArb()
    {
        header("Access-Control-Allow-Origin:*");
        $post = Yii::$app->request->post();
        $goods_id = isset($post['goods_id']) ? $post['goods_id'] : '';
        if ($goods_id) {
            $goods = Goods::findOne([$goods_id]);
            if ($goods) {
                $attr_cat_id = $goods->attr_cat_id;
            } else {
                $message = ['code' => 500, 'message' => '找不到该商品'];
                return json_encode($message);
            }
            $attrvalueARR = Goods::getArbByArcandgoods($attr_cat_id, $goods_id);
            $message = ['code' => 200, 'message' => '获取成功', 'data' => $attrvalueARR];
            return json_encode($message);
        } else {
            $message = ['code' => 500, 'message' => '缺少参数'];
            return json_encode($message);
        }
    }

    /**
     * 获取商品列表信息
     * 此接口无需传递任何验证信息
     *
     * @desc 获取商品列表信息
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionGoodslist()
    {
        $goodsList = Goods::getGoodslistInfo();
        $message = ['code' => 200, 'message' => '商品信息获取成功', 'data' => $goodsList];
        return json_encode($message);
    }

    /**
     * 根据属性值ID获取商品详情页信息第一个方式
     *
     * @desc 直接传属性值数组
     * @param array data[attrValue][] |属性ID数组|yes|
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionInfobyattr()
    {
        $post = Yii::$app->request->post();
        $postData = $post['data'];
        if ($postData) {
            $attrValueARR = $postData['attrValue'];
            $attrvalueStr = implode('', $attrValueARR);
            if ($attrvalueStr) {
                $goods_id = AttrvalueGoods::findOne(['attr_value_string' => $attrvalueStr])->goods_id;
                $goods = Goods::find()->where(['goods_id' => $goods_id])->asArray()->one();
                $goodsInfo = Goods::getGoodsInfo($goods);
                $message = ['code' => 200, 'message' => '获取成功', 'data' => $goodsInfo];
                return json_encode($message);
            }
            $message = ['code' => 500, 'message' => '属性提交错误,请检查属性提交格式!', 'data' => []];
            return json_encode($message);
        }
    }

    /**
     * 根据属性值ID获取商品详情页信息第二个方式
     *
     * @desc 拼接属性值ID，post字符串
     * @param string data[attrValue] |拼接的属性ID字符串|yes|
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionInfobyattrstring()
    {
        $post = Yii::$app->request->post();
        $postData = $post['data'];
        if ($postData) {
            $attrValueARR = $postData['attrValue'];
            $attrvalueStr = $attrValueARR;
            if ($attrvalueStr) {
                $AttrvalueGoods = AttrvalueGoods::findOne(['attr_value_string' => $attrvalueStr]);
                if ($AttrvalueGoods) {
                    $goods_id = $AttrvalueGoods->goods_id;
                } else {
                    $message = ['code' => 500, 'message' => '没有这个商品!', 'data' => []];
                    return json_encode($message);
                }
                $goods = Goods::find()->where(['goods_id' => $goods_id])->asArray()->one();
                $goodsInfo = Goods::getGoodsInfo($goods);
                $message = ['code' => 200, 'message' => '获取成功', 'data' => $goodsInfo];
                return json_encode($message);
            }
            $message = ['code' => 500, 'message' => '属性提交错误,请检查属性提交格式!', 'data' => []];
            return json_encode($message);
        }
    }

    /**
     * 获取有库存的商品
     *
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionGetgoods()
    {
//        $post = Yii::$app->request->post();
//        $postData = $post['data'];
//        $attrValueId = $postData['attr_value_id'];
        $attrValueId = '';
        $goods_idARR = AttrvalueGoods::find()->where(['attr_value_id'=>$attrValueId])->select(['goods_id'])->asArray()->all();
        $arr=array();
        foreach($goods_idARR as $k=>$v) {
            $arr[] = AttrvalueGoods::find()->where(['goods_id'=>$v['goods_id']])->select(['attr_value_id','attr_id'])->asArray()->all();
        }
        print_r($arr);exit;
    }

    /**
     * 获取推荐商品
     *
     * @desc 获取推荐商品，限制两个
     * @return array data[] 商品多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionShowgoods()
    {
        $goodsARR = Goods::find()->where(['is_delete'=>0])->limit(2)->asArray()->all();
        $message = ['code' => 200, 'message' => '获取推荐商品成功', 'data' => $goodsARR];
        return json_encode($message);
    }

    /**
     * 添加收藏商品
     *
     * @desc 类似添加购物车，但只是简单的记住这个商品，没有数量和价钱的计算
     * @param string data[goods_id] |商品ID|yes|
     * @return array data[] 属性列表多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionCollectadd()
    {
        $post = Yii::$app->request->post();
        $postData = $post['data'];

        $goods_id = $postData['goods_id'];
        $user_id = Yii::$app->UserInfo->userid;
        $collect = Collect::findOne(['goods_id'=>$goods_id,'user_id'=>$user_id]);
        if($collect) {
            $message = ['code' => 200, 'message' => '已经收藏过了', 'data' => []];
            return json_encode($message);
        } else{
            $collect = new Collect();
            $collect->goods_id = $goods_id;
            $collect->user_id = $user_id;
            if($collect->save()){
                $message = ['code' => 200, 'message' => '成功收藏', 'data' => []];
                return json_encode($message);
            }
        }


    }

    /**
     * 删除收藏商品
     *
     * @desc 删除收藏商品
     * @param string data[goods_id] |商品ID|yes|
     * @return array data[] 多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     * @return string
     */
    public function actionCollectdel()
    {
        $post = Yii::$app->request->post();
        $postData = $post['data'];
        $goods_id = $postData['goods_id'];
        $user_id = Yii::$app->UserInfo->userid;

        $collect = Collect::findOne(['goods_id'=>$goods_id,'user_id'=>$user_id]);
        if($collect->delete()){
            $message = ['code' => 200, 'message' => '删除收藏成功', 'data' => []];
            return json_encode($message);
        }
    }

    /**
     * 查看收藏的商品
     *
     * @desc 查看收藏的商品
     * @return array data[] 多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     */
    public function actionGetcollect()
    {
        $collectARR = Collect::find()
            ->select('g.*,c.rec_id')
            ->from('collect AS c')
            ->leftJoin('goods AS g','g.goods_id = c.goods_id')
            ->where(['c.user_id'=>Yii::$app->UserInfo->userid])
            ->asArray()
            ->All();
        $message = ['code' => 200, 'message' => '查看收藏数据', 'data' => $collectARR];
        return json_encode($message);
    }

    /**
     * 查看此商品是否收藏
     *
     * @desc 查看此商品是否收藏
     * @param string data[goods_id] |商品ID|yes|
     * @return array data[] 多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     */
    public  function actionIscollect()
    {
        $post = Yii::$app->request->post();
        $goods_id = $post['data']['goods_id'];
        $userid = Yii::$app->UserInfo->userid;
        if(Collect::findOne(['goods_id'=>$goods_id,'user_id'=>$userid])){
            $message = ['code' => 200, 'message' => '已收藏', 'data' => 1];
            return json_encode($message);
        }
        $message = ['code' => 200, 'message' => '未收藏', 'data' => 0];
        return json_encode($message);
    }

    /**
     * 获取首页三个商品
     *
     * @desc 获取首页三个商品
     * @return array data[] 多维数组
     * @return string message 提示信息
     * @exception 200 参数传递错误
     * @exception 500 服务器内部错误
     */
    public function actionGetindexgoods()
    {
        $goodsArr = array();
        $goodsArr[] = Goods::find()->where(['goods_id'=>'8fc674d0-bcc0-11e8-b05d-a53f0a637c51'])->asArray()->one();
        $goodsArr[] = Goods::find()->where(['goods_id'=>'b8ceeaa0-bcc1-11e8-b458-0d5aca70d5f2'])->asArray()->one();
        $goodsArr[] = Goods::find()->where(['goods_id'=>'2da50710-bcc1-11e8-86d0-c3161bb516f1'])->asArray()->one();
        $message = ['code' => 200, 'message' => '获取成功', 'data' => $goodsArr];
        return json_encode($message);
    }
}
